package org.txazo.blog.util;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;

/**
 * Http工具类
 * 
 * @author Tu XiaoZhou
 * @since 2013-08-10
 * 
 */
public class HttpUtil {

	/** 静态资源Expire的最大天数 */
	private static int expireDays = 30;
	/** 默认Refer */
	private static final String DEFAULT_REFER = "http://0.0.0.0";
	/** 静态资源后缀 */
	public static final String STATIC_RESOURCES_SUFFIX = "css^js^jpg^gif^png";

	/**
	 * 获取完整请求URL，不包含请求参数
	 * 
	 * @param url 格式(http://domainname/namespace/action.jsps?param=value)
	 * @return http://domainname/namespace/action.jsps
	 */
	public static String getRequestURLExcludeParameters(String url) {
		if (StringUtil.isNotEmpty(url) && url.startsWith("http://")) {
			int index = url.indexOf('?');
			if (index != -1) {
				return url.substring(0, index);
			} else {
				if (url.endsWith("/")) {
					return url.substring(0, url.length() - 1);
				} else {
					return url;
				}
			}
		}
		return null;
	}

	/**
	 * 获取完整请求URL，包含请求参数
	 * 
	 * @return http://domainname/namespace/action.jsps?param=value
	 */
	public static String getRequestURLIncludeParameters(
			HttpServletRequest request) {
		String url = request.getRequestURL().toString();
		String queryString = request.getQueryString();
		if (StringUtil.isNotEmpty(queryString)) {
			url += "?" + queryString;
		}

		return url;
	}

	/**
	 * URI是否静态资源
	 * 
	 * @param uri 静态资源格式(/*.css | /*.js | /*.jpg | /*.gif | /*.png)
	 */
	public static boolean isStaticResourceOfURI(String uri) {
		if (StringUtil.isNotEmpty(uri) && uri.contains(".")) {
			String type = uri.substring(uri.lastIndexOf(".") + 1, uri.length());
			if (STATIC_RESOURCES_SUFFIX.contains(type)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * URI是否动态请求
	 * 
	 * @param uri 动态请求格式(/namespace/action.jsps)
	 */
	public static boolean isDynamicRequestOfURI(String uri) {
		if (StringUtil.isNotEmpty(uri) && uri.endsWith("jsps")) {
			return true;
		}
		return false;
	}

	/**
	 * 返回完整URL
	 * 
	 * @param uri 格式(/namespace/action.jsps)
	 * @return http://domainname/namespace/action.jsps
	 */
	public static String getFullRedirectURL(String uri) {
		return ContextUtil.getBaseHref() + uri;
	}

	/** 返回请求的Refer */
	public static String getRequestRefer(HttpServletRequest request) {
		String refer = request.getHeader("Referer");
		if (StringUtil.isEmpty(refer)) {
			refer = DEFAULT_REFER;
		} else {
			refer = getRequestURLExcludeParameters(refer);
		}
		return refer;
	}

	/** 获取静态资源的Expire时间(格式EEE, dd MMM yyyy HH:mm:ss GMT) */
	public static String getExpireTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, expireDays);
		return DateFormatUtil.getGMTDateTime(calendar.getTime());
	}

	/** 获取请求真实IP */
	public static String getRemoteIp(HttpServletRequest request) {
		/** CDN */
		String ip = request.getHeader("Cdn-Src-Ip");
		if (StringUtil.isNotEmpty(ip)) {
			return ip;
		}

		/** 反向代理 */
		ip = request.getHeader("X-Real-IP");
		if (StringUtil.isNotEmpty(ip)) {
			return ip;
		}

		/** 反向代理 */
		ip = request.getHeader("X-Forwarded-For");
		if (StringUtil.isNotEmpty(ip)) {
			return ip;
		}

		/** 代理服务器 */
		ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		if (StringUtil.isNotEmpty(ip)) {
			return ip;
		}

		ip = request.getHeader("REMOTE_ADDR");
		if (StringUtil.isNotEmpty(ip)) {
			return ip;
		}

		if (StringUtil.isEmpty(ip)) {
			ip = request.getRemoteAddr();
		}

		return ip;
	}

}
